package com.mlnx.yingbaisui.device.utils;

import java.util.*;

public class CommonDevice {

    //========================55aa00 通讯检测(心跳)========================

    public static String sendCommDetection() {
        String tmp = "55aa0000";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveCommDetection(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data);
        return resultMap;
    }

    //========================55aa01  查下产品信息========================

    public static String sendQueryProduct() {
        String tmp = "55aa0100";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    //========================55aa02 初始化========================

    public static String sendInitDevice() {
        String tmp = "55aa0200";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveInitDevice(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data);
        return resultMap;
    }

    //========================55aa03  获取本地时间========================

    /**
     * 【功能：获取本地时间】:
     * 【答：主端】：发送55aa03指令+时间+最后校验和
     * 【问：从端】：发送 55aa03+设备ID+最后校验和
     * @return
     */
    public static String sendServiceTime(Boolean status) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int year =  cal.get(Calendar.YEAR) - 2000;
        int month =  cal.get(Calendar.MONTH) + 1;
        int day =  cal.get(Calendar.DAY_OF_MONTH);
        int hour =  cal.get(Calendar.HOUR_OF_DAY);
        int minute =  cal.get(Calendar.MINUTE);
        int second =  cal.get(Calendar.SECOND);
        int week = cal.get(Calendar.WEEK_OF_MONTH);
        String tmp = "55aa030901" + StringUtils.toHexStr(year) + StringUtils.toHexStr(month) + StringUtils.toHexStr(day)
                + StringUtils.toHexStr(hour) + StringUtils.toHexStr(minute) + StringUtils.toHexStr(second) + StringUtils.toHexStr(week) + (status ? "5a" : "00");
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }


    public static HashMap<String, Object> receiveTimeResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        return resultMap;
    }

    //========================55aa04 设备重启========================

    public static String sendResetDevice() {
        String tmp = "55aa0400";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveResetDevice(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        return resultMap;
    }

    //========================55aa05 设备连接时间========================

    public static String sendQueryConnectEventTime() {
        String tmp = "55aa0500";
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveDeviceTimeResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("lastRouterConnectTime-Y", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("lastRouterConnectTime-M", Integer.parseInt(data.substring(14, 16), 16));
        resultMap.put("lastRouterConnectTime-D", Integer.parseInt(data.substring(16, 18), 16));
        resultMap.put("lastRouterConnectTime-h", Integer.parseInt(data.substring(18, 20), 16));
        resultMap.put("lastRouterConnectTime-m", Integer.parseInt(data.substring(20, 22), 16));
        resultMap.put("lastRouterConnectTime-s", Integer.parseInt(data.substring(22, 24), 16));
        resultMap.put("lastRouterBreakTime-Y", Integer.parseInt(data.substring(24, 26), 16));
        resultMap.put("lastRouterBreakTime-M", Integer.parseInt(data.substring(26, 28), 16));
        resultMap.put("lastRouterBreakTime-D", Integer.parseInt(data.substring(28, 30), 16));
        resultMap.put("lastRouterBreakTime-h", Integer.parseInt(data.substring(30, 32), 16));
        resultMap.put("lastRouterBreakTime-m", Integer.parseInt(data.substring(32, 34), 16));
        resultMap.put("lastRouterBreakTime-s", Integer.parseInt(data.substring(34, 36), 16));
        resultMap.put("lastSocketConnectTime-Y", Integer.parseInt(data.substring(36, 38), 16));
        resultMap.put("lastSocketConnectTime-M", Integer.parseInt(data.substring(38, 40), 16));
        resultMap.put("lastSocketConnectTime-D", Integer.parseInt(data.substring(40, 42), 16));
        resultMap.put("lastSocketConnectTime-h", Integer.parseInt(data.substring(42, 44), 16));
        resultMap.put("lastSocketConnectTime-m", Integer.parseInt(data.substring(44, 46), 16));
        resultMap.put("lastSocketConnectTime-s", Integer.parseInt(data.substring(46, 48), 16));
        resultMap.put("lastSocketBreakTime-Y", Integer.parseInt(data.substring(48, 50), 16));
        resultMap.put("lastSocketBreakTime-M", Integer.parseInt(data.substring(50, 52), 16));
        resultMap.put("lastSocketBreakTime-D", Integer.parseInt(data.substring(52, 54), 16));
        resultMap.put("lastSocketBreakTime-h", Integer.parseInt(data.substring(54, 56), 16));
        resultMap.put("lastSocketBreakTime-m", Integer.parseInt(data.substring(56, 58), 16));
        resultMap.put("lastSocketBreakTime-s", Integer.parseInt(data.substring(58, 60), 16));
        resultMap.put("error-code", Integer.parseInt(data.substring(60, 62), 16));
        resultMap.put("device-signal", Integer.parseInt(data.substring(62, 64), 16));
        return resultMap;
    }

    //========================55aaef wifi信息========================

    public static String sendWifi(String name, String password) {
        String nameU = StringUtils.enUnicode(name);
        String passwordU = StringUtils.enUnicode(password);
        int len = nameU.length() / 2 + passwordU.length() / 2 + 2;
        String tmp ="55aaef" + StringUtils.toHexStr(len)+ StringUtils.toHexStr(nameU.length() / 2) + StringUtils.toHexStr(passwordU.length() / 2) + nameU + passwordU;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveWifiResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aaf1 ip信息========================

    public static String sendIp(String ip) {
        String ipU =  StringUtils.enUnicode(ip);
        String tmp ="55aaf1" + StringUtils.toHexStr(ipU.length() / 2) + ipU;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveIpResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aaf2 子网掩码信息========================

    public static String sendMask(String mask) {
        String maskU = StringUtils.enUnicode(mask);
        String tmp ="55aaf2" + StringUtils.toHexStr(maskU.length() / 2) + maskU;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveMaskResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aaf3 网关信息========================

    public static String sendGateway(String gateway) {
        String gatewayU = StringUtils.enUnicode(gateway);
        String tmp ="55aaf3" + StringUtils.toHexStr(gatewayU.length() / 2) + gatewayU;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveGatewayResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aaf4 IP分配信息========================

    public static String sendIpType(int type) {
        String tmp ="55aaf401" + StringUtils.toHexStr(type);
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveIpTypeResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("type", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aafa 无人上报开关========================

    public static String sendUnmannedEscalation(int isTrue) {
        String tmp ="55aafa01" + StringUtils.toHexStr(isTrue);
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveUnmannedEscalation(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("unmannedEscalation", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //========================55aaf9  设置上报频次========================

    public static String sendEscalationFrequency(int offlineSum, int onlineSum) {
        String tmp ="55aaf902" + StringUtils.toHexStr(offlineSum) + StringUtils.toHexStr(onlineSum);
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static HashMap<String, Object> receiveEscalationFrequency(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("offlineEscalationFrequency", Integer.parseInt(data.substring(12, 14), 16));
        resultMap.put("onlineEscalationFrequency", Integer.parseInt(data.substring(14, 16), 16));
        return resultMap;
    }

    //========================55aafe 设备断电信息========================

    public static HashMap<String, Object> receiveDeviceOutage(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        return resultMap;
    }

    //=======================55aafd 服务器信息=========================

    //设置服务器 不重启设备 并且不会有回复
    public static String sendServer(String address, int port) {
        String tmp = "AT+SERVER=" + address + "," + port;
        return StringUtils.enUnicode(tmp) + "0d0a";
    }

    public static String sendServer(String address, String port) {
        String addressU =  StringUtils.enUnicode(address + "," + port);
        String tmp = "55aafd" + StringUtils.toHexStr(addressU.length() / 2) + addressU;
        tmp += StringUtils.addLast2Hex(tmp);
        return tmp;
    }

    public static  HashMap<String, Object> receiveServerResult(String data) {
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("deviceId", data.substring(0, 12));
        resultMap.put("result", Integer.parseInt(data.substring(12), 16));
        return resultMap;
    }

    //======================== AT+OTA 设备升级========================

    public static String sendOTA(String path) {
        String tmp = "AT+OTA=" + path;
        return StringUtils.enUnicode(tmp) + "0d0a";
    }

}
